$(document).ready(function() {
    	var whereClause={"all":"all"};
    	$.ajax({
    		url:'http://localhost:8080/easyskool/rest/masterservice/getSchoolRequests',
    		type:'POST',
    		data: '{"user":"user","whereClause":'+null+'}',
    		dataType: 'json',
    		contentType: "application/json; charset=utf-8",
    		success:function (data,testStatus,response) {
    			var jsondata =JSON.parse(response.responseText);
    			$.each(jsondata, function(i, item) {
    			$("#mytable").append("<tr id = " + i + ">" +
    								 "<td><input type='checkbox' name='checkboxselect' title='Select All' id="+i+" /></td>"+
    								 "<td class='schoolCode'>" + item.schoolCode	+ "</td>" +
			    					 "<td class='scName'><a id='showRequest' data-toggle='modal' data-target='#myShowModal'>" + item.scName + "</a></td>" +
			    					 "<td class='scCity'>" + item.scCity	+ "</td>" +
			    					 "<td class='scAffliation'>"	+ item.scAffliation + "</td>" +
			    					 "<td class='admFname'>"	+ item.admFname + "</td>" +
			    					 "<td class='admEmail'>" + item.admEmail	+ "</td>" +
			    					 "<td class='scAddress' hidden='true'>" + item.scAddress	+ "</td>" +
			    					 "<td class='scCity' hidden='true'>" + item.scCity	+ "</td>" +
			    					 "<td class='scState' hidden='true'>" + item.scState	+ "</td>" +
			    					 "<td class='scCountry' hidden='true'>" + item.scCountry	+ "</td>" +
			    					 "<td class='scPin' hidden='true'>" + item.scPin	+ "</td>" +
			    					 "<td class='scNoOfStudents' hidden='true'>" + item.scNoOfStudents	+ "</td>" +
			    					 "<td class='admMname' hidden='true'>" + item.admMname	+ "</td>" +
			    					 "<td class='admLname' hidden='true'>" + item.admLname	+ "</td>" +
			    					 "<td class='admAddress' hidden='true'>" + item.admAddress	+ "</td>" +
			    					 "<td class='admCity' hidden='true'>" + item.admCity	+ "</td>" +
			    					 "<td class='admState' hidden='true'>" + item.admState	+ "</td>" +
			    					 "<td class='admCountry' hidden='true'>" + item.admCountry	+ "</td>" +
			    					 "<td class='admPin' hidden='true'>" + item.admPin	+ "</td>" +
			    					 "<td class='aaPassword' hidden='true'>" + item.aaPassword	+ "</td>" +
			    					 "<td class='aaSecurityQ1' hidden='true'>" + item.aaSecurityQ1	+ "</td>" +
			    					 "<td class='aaSecurityA1' hidden='true'>" + item.aaSecurityA1	+ "</td>" +
			    					 "<td class='aaSecurityQ2' hidden='true'>" + item.aaSecurityQ2	+ "</td>" +
			    					 "<td class='aaSecurityA2' hidden='true'>" + item.aaSecurityA2	+ "</td>" +
			    					 "<td class='aaMobileNumber' hidden='true'>" + item.aaMobileNumber	+ "</td>" +
			    					 "<td class='createdDate'>" + item.createdDate + "</td>" +
			    					 "<td class='requestStatus'>" + item.requestStatus	+ "</td>" +
			    					 "<td class='requestStatusChangedBy'>" + item.requestStatusChangedBy	+ "</td>" +
			    					 "<td class='requestStatusChangedDate'>" + item.requestStatusChangedDate	+ "</td>" +
			    					 "<td><input type='button' value='Approve' class='btn btn-info' id='approve' data-toggle='modal' data-target='#myConfirmModal' /></td>" +
			    					 "<td><input type='button' value='Reject' class='btn btn-danger' id='reject' data-toggle='modal' data-target='#myConfirmModal'/></td></tr>");
    			});
    			
    			}
    	});
	});

	$('.form-fields').on('click', '#showRequest', function(){
		var curRowId = $(this).closest('tr').attr("id");
	    $('input[id=schoolCode]').val		( $('#' + curRowId + ' td.schoolCode').text() );
	    $('input[id=requestStatus]').val	( $('#' + curRowId + ' td.requestStatus').text() );
	    $('input[id=scName]').val	( $('#' + curRowId + ' td.scName').text() );
	    $('input[id=scAddress]').val		( $('#' + curRowId + ' td.scAddress').text() );
	    $('input[id=scCity]').val	( $('#' + curRowId + ' td.scCity').text() );
	    $('input[id=scState]').val		( $('#' + curRowId + ' td.scState').text() );
	    $('input[id=scCountry]').val	( $('#' + curRowId + ' td.scCountry').text() );
	    $('input[id=scPin]').val	( $('#' + curRowId + ' td.scPin').text() );
	    $('input[id=scNoOfStudents]').val		( $('#' + curRowId + ' td.scNoOfStudents').text() );
	    $('input[id=scAffliation]').val	( $('#' + curRowId + ' td.scAffliation').text() );
	    $('input[id=admFname]').val		( $('#' + curRowId + ' td.admFname').text() );
	    $('input[id=admMname]').val	( $('#' + curRowId + ' td.admMname').text() );
	    $('input[id=admLname]').val	( $('#' + curRowId + ' td.admLname').text() );
	    $('input[id=admAddress]').val		( $('#' + curRowId + ' td.admAddress').text() );
	    $('input[id=admCity]').val	( $('#' + curRowId + ' td.admCity').text() );
	    $('input[id=admState]').val		( $('#' + curRowId + ' td.admState').text() );
	    $('input[id=admCountry]').val	( $('#' + curRowId + ' td.admCountry').text() );
	    $('input[id=admPin]').val	( $('#' + curRowId + ' td.admPin').text() );
	    $('input[id=admEmail]').val		( $('#' + curRowId + ' td.admEmail').text() );
	    $('input[id=aaSecurityQ1]').val	( $('#' + curRowId + ' td.aaSecurityQ1').text() );
	    $('input[id=aaSecurityA1]').val	( $('#' + curRowId + ' td.aaSecurityA1').text() );
	    $('input[id=aaSecurityQ2]').val	( $('#' + curRowId + ' td.aaSecurityQ2').text() );
	    $('input[id=aaSecurityA2]').val	( $('#' + curRowId + ' td.aaSecurityA2').text() );
	    $('input[id=aaMobileNumber]').val	( $('#' + curRowId + ' td.aaMobileNumber').text() );
	    $('input[id=aaPassword]').val	( $('#' + curRowId + ' td.aaPassword').text() );
	    $('input[id=requestStatusChangedBy]').val	( $('#' + curRowId + ' td.requestStatusChangedBy').text() );
	    $('input[id=requestStatusChangedDate]').val	( $('#' + curRowId + ' td.requestStatusChangedDate').text() );
	    $('input[id=createdDate]').val	( $('#' + curRowId + ' td.createdDate').text() );
	});
	
	$('.form-fields').on('click', '#reject', function(){
    	
    	var curRowId = $(this).closest('tr');
    	$("#myConfirmModal").modal('show');
    	
    	$('#confirmModalButton').click(function() {
    		$("#myConfirmModal").modal('hide');
           	
           	$.ajax({
           		url : 'http://localhost:8080/easyskool/rest/masterservice/rejectSchoolRequest',
       			type : 'POST',
       			data : '{"user":"aa","whereClause":'+null+',"updateClause":' + tableRowToObj(curRowId) + '}',
       			dataType : 'json',
       			contentType : "application/json; charset=utf-8",
       			success : function(data,testStatus,response) {
       				var json = jQuery.parseJSON(response.responseText);
       				if(json.update == "success"){
       					window.location = "http://localhost:8080/easyskool/WebPages/Masters/masterApproveSchoolRequest.jsp";
       				} else	{
    				alert("Error: ! "+ jqXHR.responseText);
       				}
       			}
           	});
           	
            
        });
    	
    	$('#cancelModalButton').click(function() {
    		$("#myConfirmModal").modal('hide');
    	
    	});
    	
    });

	$('.form-fields').on('click', '#approve', function(){
    	
    	var curRowId = $(this).closest('tr');
    	$("#myConfirmModal").modal('show');
    	
    	$('#confirmModalButton').click(function() {
    		$("#myConfirmModal").modal('hide');
       	
           	$.ajax({
           		url : 'http://localhost:8080/easyskool/rest/masterservice/approveSchoolRequest',
       			type : 'POST',
       			data : '{"user":"aa","whereClause":'+null+',"updateClause":' + tableRowToObj(curRowId) + '}',
				dataType : 'json',
				contentType : "application/json; charset=utf-8",
				success : function(data,testStatus,response) {
					var json = jQuery.parseJSON(response.responseText);
						if(json.update == "success"){
							window.location = "http://localhost:8080/easyskool/WebPages/Masters/MasterSectionHome.jsp";
						} else{
							alert("Error: ! "+ jqXHR.responseText);
						}
				}
           	});
    	
    	
        });
    	
    	$('#cancelModalButton').click(function() {
    		$("#myConfirmModal").modal('hide');
    	
    	});
    	
    });
	
